import cv2
import HandMoudle as hm
import time
import os
import numpy as np


ImgPath = 'imgPath'
my_imgs = os.listdir(ImgPath)
img_list = []
for img in my_imgs:
    im = cv2.imread(f'{ImgPath}/{img}')
    img_list.append(im)

#################################################
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
Ptime = 0
detector = hm.HandMoudle()
halmlist = [4,8,12,16,20]
while True:
    ret,frame = cap.read()
    frame= detector.findHands(frame)
    lmlist = detector.findPosition(frame,draw=False)
    # print(lmlist)
    if len(lmlist) != 0:
        handnum = []
        if lmlist[halmlist[0]][1] < lmlist[halmlist[0] - 1][1]:
            handnum.append(1)
        else:
            handnum.append(0)
        for id in range(1,5):
            if lmlist[halmlist[id]][2] < lmlist[halmlist[id]-2][2]:
                handnum.append(1)
            else:
                handnum.append(0)
        nums = handnum.count(1)
        # print(handnum)

        h, w, c = img_list[nums-1].shape
        frame[0:h, 0:w] = img_list[nums-1]

    Ctime = time.time()
    fps = 1/(Ctime-Ptime)
    Ptime = Ctime
    cv2.putText(frame,'FPS:'+str(int(fps)),(400,70),cv2.FONT_HERSHEY_PLAIN,3,(255,0,255),3)
    cv2.imshow('frame',frame)
    cv2.waitKey(1)